# coding: utf-8

__author__ = 'xnny'

from HTMLParser import HTMLParser
from xml.dom.minidom import parse, parseString
from rules import parsingOptions, skipStuffFromDomains__links, keepStuffFromDomain__video, isSkipStuffFroDomainsLinks\
,isSkipStuffFromDomainImages, measureText__getTextLength


class getContent__exploreNodeAndGetStuff:
    ss = 0

    _global__element_index = 0

    _global__inside_link = False
    _global__inside_link__element_index = 0

    _global__length__above_plain_text = 0
    _global__count__above_plain_words = 0
    _global__length__above_links_text = 0      # 链接文本长度
    _global__count__above_links_words = 0      # 有多少链接纯文本
    _global__count__above_candidates = 0
    _global__count__above_containers = 0
    _global__above__plain_text = ''
    _global__above__links_text = ''

    _return__containers = []
    _return__candidates = []
    _return__links = []

    def __init__(self, _node, _justExploring):
        self._recursive(_node, _justExploring)

    def _recursive(self, _node, _justExploring):

        self._global__element_index +=  1
        _nodeType = _node.nodeType
        if _nodeType == 3 :
            _tag_name = '#text'
        else:
            if _nodeType == 1 and _node.nodeName and _node.nodeName > '':
                _tag_name = _node.nodeName.lower()
            else:
                _tag_name = '#invalid'

        _result = {
            '__index' : self._global__element_index,
            '__node' : _node,

            '_is__container' : (parsingOptions['_elements_container'].find(''.join(['|', _tag_name, '|'])) > -1),
            '_is__candidate' : False,
            '_is__text' : False,
            '_is__link' : False,
            '_is__link_skip' : False,
            '_is__image_small' : False,
            '_is__image_medium' : False,
            '_is__image_large' : False,
            '_is__image_skip' : False,

            # debug
            '_length__above_plain_text' : self._global__length__above_plain_text,
            '_count__above_plain_words' : self._global__count__above_plain_words,

            '_length__above_links_text' : self._global__length__above_links_text,
            '_count__above_links_words' : self._global__count__above_links_words,

            '_length__above_all_text' : (self._global__length__above_plain_text + self._global__length__above_links_text),
            '_count__above_all_words' : (self._global__count__above_plain_words + self._global__count__above_links_words),

            '_count__above_candidates' : self._global__count__above_candidates,
            '_count__above_containers' : self._global__count__above_containers,

            '_length__plain_text' : 0,
            '_count__plain_words' : 0,

            '_length__links_text' : 0,
            '_count__links_words' : 0,

            '_length__all_text' : 0,
            '_count__all_words' : 0,

            '_count__containers' : 0,
            '_count__candidates' : 0,

            '_count__links' : 0,
            '_count__links_skip' : 0,

            '_count__images_small' : 0,
            '_count__images_medium' : 0,
            '_count__images_large' : 0,
            '_count__images_skip' : 0
        }

        #        第一波
        if _tag_name == '#invalid' :
            return False
        elif parsingOptions['_elements_ignore'].find(''.join(['|', _tag_name , '|'])) > -1:
            return False
        elif parsingOptions['_elements_visible'].find(''.join(['|', _tag_name , '|'])) > -1:
            s = ''
        #            判断元素的位置
        elif parsingOptions['_elements_self_closing'].find(''.join(['|', _tag_name , '|'])) > -1:
            if _tag_name == 'img':
                s = ''
            else :
                return False
            s = ''

        #        第二波
        if _tag_name == '#text':
            _result['_is__text'] = True

            _nodeText = _node.nodeValue
            _result['_length__plain_text'] = measureText__getTextLength(_nodeText)
            _result['_count__plain_words'] = measureText__getTextLength(_nodeText)  # 修改1 measureText__getWordCount

            if self._global__inside_link :
                self._global__length__above_links_text += _result['_length__plain_text']
                self._global__count__above_links_words += _result['_count__plain_words']
            #                if (false && $R.debug) { _global__above__links_text += ' ' + _nodeText; }
            else:
                self._global__length__above_plain_text += _result['_length__plain_text']
                self._global__count__above_plain_words += _result['_count__plain_words']
            return _result

        elif _tag_name == 'a':
            _href = _node.href
            if _href > '':                  # if (_href.indexOf); else { break; }
                _result['_is__link'] = True

                if isSkipStuffFroDomainsLinks(_href):
                    _result['_is__link_skip'] = True
                else:
                    if self._global__inside_link:
                        s = ''
                    else:
                        self._global__inside_link = True
                        self._global__inside_link__element_index = _result['__index']
#                    完成
                    self._return__links.append(_result)

        elif _tag_name == 'img':
            _src = _tag_name.src
            if _src:
                if isSkipStuffFromDomainImages(_src) :
                    _result['_is__image_skip'] = True
                    #            SIZE
                    #            判断图像高宽。。

        for _child in _node.childNodes: # 需要重要敲定下
            print(_child)
#            return False
            _child_result = self._recursive(_child, _justExploring)
            if _child_result :
                s = ''
            else :
                continue

            if _child_result['_is__link'] == 1 :
                _result['_count__links'] += _child_result['_count__links'] + 1
            else :
                _result['_count__links'] += _child_result['_count__links']
            if _child_result['_is__link_skip'] == 1 :
                _result['_count__links_skip'] += _child_result['_count__links_skip'] + 1
            else :
                _result['_count__links_skip'] += _child_result['_count__links_skip']

            if _child_result['_is__container'] == 1:
                _result['_count__containers'] += _child_result['_count__containers'] + 1
            else :
                _result['_count__containers'] += _child_result['_count__containers']
            if _child_result['_is__candidate'] == 1:
                _result['_count__candidates'] += _child_result['_count__candidates'] + 1
            else :
                _result['_count__candidates'] += _child_result['_count__candidates']

            _result['_length__all_text'] += _child_result['_length__plain_text'] + _child_result['_length__links_text']
            _result['_count__all_words'] += _child_result['_count__plain_words'] + _child_result['_count__links_words']

            if _result['_is__link'] :
                _result['_length__links_text'] += _child_result['_length__plain_text'] + _child_result['_length__links_text']
                _result['_count__links_words'] += _child_result['_count__plain_words'] + _child_result['_count__links_words']
            else :
                _result['_length__plain_text'] += _child_result['_length__plain_text']
                _result['_count__plain_words'] += _child_result['_count__plain_words']
                _result['_length__links_text'] += _child_result['_length__links_text']
                _result['_count__links_words'] += _child_result['_count__links_words']

        if _result['_is__link'] and self._global__inside_link__element_index == _result['__index'] :
            _global__inside_link = False
            _global__inside_link__element_index = 0

        if _result['_is__container'] or (_result['__index'] == 1 and _justExploring == True) :
            self._return__containers.append(_result)
            if _result['_is__container'] :
                self._global__count__above_containers += 1

            if _justExploring :
                s = ''
            else :
                if _result['_count__links'] * 2 >= _result['_count__plain_words'] :
                    s = ''
                elif _result['_length__plain_text'] < (65 / 3) :
                    s = ''
                elif _result['_count__plain_words'] < 5 :
                    s = ''
                elif _result['_length__plain_text'] < 10 :
                    s = ''
                elif _result['_count__plain_words'] < 2 :
                    s = ''
                else :
                    _result['_is__candidate'] = True
                    self._return__candidates.append(_result)
                    self._global__count__above_candidates += 1

            if _result['__index'] == 1 and not(_result['_is__candidate']) :
                _result['_is__candidate'] = True
                _result['_is__bad'] = True

                self._return__candidates.append(_result)

        return _result




#        self._recursive()
string = '''<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="http://localhost/static/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="http://localhost/static/style/style.css?v=20101102" rel="stylesheet" type="text/css" media="screen" />
<title>我的订阅</title>
<style>
#news-bottom-list { text-align:center; }
</style>
</head>
<body>
<div></div>
ssfssdsfsfs
<div></div>
</body>
</html>'''

dom = parseString(string)
_node = dom.getElementsByTagName('body')[0]    # test
ss = getContent__exploreNodeAndGetStuff(_node, False)


